package com.hsd.mapper.impl;

import com.hsd.mapper.IAccountDao;
import com.hsd.entity.Account;
import org.hibernate.Session;
import org.hibernate.SessionFactory;
import org.hibernate.query.Query;
import org.springframework.stereotype.Repository;

import javax.annotation.Resource;
import java.util.List;

@Repository
public class IAccountDaoImpl implements IAccountDao {
    @Resource
    private SessionFactory sessionFactory;

    private Session getSession() {
        return sessionFactory.getCurrentSession();
    }

    @Override
    public Account selectByAcc(String acc) {
        return getSession().get(Account.class, acc);
    }

    @Override
    public int updateMoney(String acc, Double money) {
        String hql="update Account acc set acc.money = acc.money + :m where acc.acc = :a";
        Session session = getSession();
        Query query = session.createQuery(hql);
        //设置占位参数
        query.setParameter("m",money);
        query.setParameter("a",acc);
        return query.executeUpdate();
    }

    @Override
    public List<Account> selectAll() {
        String hql = "from Account acc";
        Session session = getSession();
        Query query = session.createQuery(hql);
        return query.list();
    }
}
